/**
 * Copyright &copy; 2016 CNJSON All rights reserved.
 */
package com.hongsuan.pplive.modules.live.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.cnjson.db.persistence.Page;
import com.cnjson.common.utils.StringUtils;
import com.cnjson.common.web.BaseController;
import com.cnjson.common.config.Global;
import com.hongsuan.pplive.modules.live.entity.LiveIncomeStrategy;
import com.hongsuan.pplive.modules.live.service.LiveIncomeStrategyService;

/**
 * 单表生成Controller
 * 
 * @author cgli
 * @version 2016-04-18
 */
@Controller
@RequestMapping(value = "${adminPath}/live/liveIncomeStrategy")
public class LiveIncomeStrategyController extends BaseController {

	@Autowired
	private LiveIncomeStrategyService liveIncomeStrategyService;

	@ModelAttribute
	public LiveIncomeStrategy get(@RequestParam(required = false) String id) {
		LiveIncomeStrategy entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = liveIncomeStrategyService.get(id);
		}
		if (entity == null) {
			entity = new LiveIncomeStrategy();
		}
		return entity;
	}

	@RequiresPermissions("live:liveIncomeStrategy:view")
	@RequestMapping(value = { "list", "" })
	public String list(LiveIncomeStrategy liveIncomeStrategy, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Page<LiveIncomeStrategy> page = liveIncomeStrategyService
				.findPage(new Page<LiveIncomeStrategy>(request, response), liveIncomeStrategy);
		model.addAttribute("page", page);
		return "modules/live/liveIncomeStrategyList";
	}

	@RequiresPermissions("live:liveIncomeStrategy:view")
	@RequestMapping(value = "form")
	public String form(LiveIncomeStrategy liveIncomeStrategy, String userId, Model model) {
		model.addAttribute("liveIncomeStrategy", liveIncomeStrategy);
		model.addAttribute("userId", userId);
		return "modules/live/liveIncomeStrategyForm";
	}

	@RequiresPermissions("live:liveIncomeStrategy:edit")
	@RequestMapping(value = "save")
	public String save(LiveIncomeStrategy liveIncomeStrategy, Model model, RedirectAttributes redirectAttributes) {
		String userId = "";
		if (liveIncomeStrategy != null && liveIncomeStrategy.getUserId()!=null) {
			userId = liveIncomeStrategy.getUserId();
		}
		if (!beanValidator(model, liveIncomeStrategy)) {
			return form(liveIncomeStrategy, userId, model);
		}
		liveIncomeStrategyService.save(liveIncomeStrategy);
		addMessage(redirectAttributes, "保存单表成功");
		return "redirect:" + Global.getAdminPath() + "/live/liveIncomeStrategy/?repage";
	}

	@RequiresPermissions("live:liveIncomeStrategy:edit")
	@RequestMapping(value = "delete")
	public String delete(LiveIncomeStrategy liveIncomeStrategy, RedirectAttributes redirectAttributes) {
		liveIncomeStrategyService.delete(liveIncomeStrategy);
		addMessage(redirectAttributes, "删除单表成功");
		return "redirect:" + Global.getAdminPath() + "/live/liveIncomeStrategy/?repage";
	}

}